package com.resource.base.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;

@Slf4j
@Component
@Aspect
public class ConsoleReportAspect {

    private static final String title = "\nSpringBoot action report -------- ";

    private static int maxOutputLengthOfParaValue = 512;

    private static final ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    @Pointcut("(within(@org.springframework.stereotype.Controller *)"
            + " || within(@org.springframework.web.bind.annotation.RestController *)))")
    public void c() {
    }

    @Around("c()")
    public Object a(ProceedingJoinPoint point) throws Throwable {
        //拿到HttpServletRequest
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        if (ra instanceof  ServletRequestAttributes){
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            HttpServletRequest request = sra.getRequest();

            Signature signature = point.getSignature();
            StringBuffer sb = new StringBuffer();
            sb.append(title).append(sdf.get().format(new Date())).append(" --------------------------\n");
            sb.append("Url         :").append(request.getMethod()).append("     ").append(request.getRequestURI()).append("\n");
            sb.append("Controller  : ").append(signature.getDeclaringTypeName()).append(".(")
                    .append(signature.getDeclaringType().getSimpleName()).append(".java:1)\n");
            sb.append("Method      : ").append(signature.getName()).append("\n");
            sb.append("Args        : ").append(Arrays.toString(point.getArgs())).append("\n");
            Enumeration<String> e = request.getParameterNames();
            if (e.hasMoreElements()){
                sb.append("Parameter   : ");
                while (e.hasMoreElements()){
                    String name = e.nextElement();
                    String[] values = request.getParameterValues(name);
                    if (values.length==1){
                        sb.append(name).append("=");
                        if (values[0] != null && values[0].length() > maxOutputLengthOfParaValue) {
                            sb.append(values[0].substring(0, maxOutputLengthOfParaValue)).append("...");
                        } else {
                            sb.append(values[0]);
                        }
                    }else{
                        sb.append(name).append("[]={");
                        for (int i = 0; i < values.length; i++) {
                            if (i > 0)
                                sb.append(",");
                            sb.append(values[i]);
                        }
                        sb.append("}");
                    }
                    sb.append("     ");
                }
                sb.append("\n");
            }
            sb.append("--------------------------------------------------------------------------------\n");
            System.out.println(sb.toString());

        }
        return point.proceed();
    }

}



